package org.bw.bookstore.book.service.impl;

import org.bw.bookstore.book.mapper.ShopMapper;
import org.bw.bookstore.book.service.ShopService;
import org.bw.bookstore.common.entity.TbShop;
import org.bw.bookstore.common.utils.ResponseResult;
import org.bw.bookstore.common.utils.StringTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program:server-store
 * @description:门店实现类
 * @author:guoshuai
 * @create:2020-10-27 09:02
 **/
@Service
public class ShopServiceimpl implements ShopService {
    @Autowired
    ShopMapper shopMapper;

    @Override
    public ResponseResult addShop(TbShop shop) {
        ResponseResult result = ResponseResult.SUCCESS();
        //添加创建时间
        Date date = new Date();
        shop.setCreateTime(date);
        shop.setCode(StringTools.getRandomString(32));
        int insert = shopMapper.insert(shop);

        if (insert < 0) {
            result.setMessage("门店添加失败");
        }
        return result;
    }

    @Override
    public ResponseResult deleteShop(Integer shopId) {
        ResponseResult result = ResponseResult.SUCCESS();
        TbShop tbShop = new TbShop();
        //生成唯一标识 改变状态
        tbShop.setId(shopId);
        tbShop.setDeleted(1);
        int insert = shopMapper.updateById(tbShop);
        if (insert < 0) {
            result.setMessage("门店下架失败");
        }
        return result;
    }

    @Override
    public ResponseResult updateShop(TbShop shop) {
        ResponseResult result = ResponseResult.SUCCESS();
        Date date = new Date();
        shop.setUpdateTime(date);
        int i = shopMapper.updateById(shop);
        if (i < 0) {
            result.setMessage("门店修改失败");
        }
        return result;
    }

    @Override
    public ResponseResult listShop() {
        ResponseResult result = ResponseResult.SUCCESS();
        Map<String, Object> resultMap = new HashMap<>();
        List<TbShop> shopList = shopMapper.getShop();
        resultMap.put("shopList",shopList);
        result.setResult(resultMap);
        return result;
    }
}